process.env.NODE_ENV = 'dev';
/*************引入模块*****************/
const conf=require('../conf.js');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const glob = require('glob');
const express = require('express');
const webpack = require('webpack')
const path=require('path');
const dev = require('./base.js');
const plugins=require('../base/plugins/index.js');
const fs=require('fs');
const opn=require('opn');
/*************定义变量*****************/
var openBom=conf.dev.openBom;
var devstart=conf.yourApps[conf.dev.devStart];
var app = express();
var entry=dev.entry;
for(var key in entry){
	entry[key]=[entry[key]];
	entry[key].unshift('webpack-hot-middleware/client?noInfo=true&reload=true')
}
/*************执行方法*****************/
var compiler = webpack(dev);
var devMiddleware = require('webpack-dev-middleware')(compiler, {
	publicPath:"/",
	stats:{
		colors:true,
		chunks:false
	}
});
var hotMiddleware =require("webpack-hot-middleware")(compiler,{
	path:"/__webpack_hmr"
})

app.use(devMiddleware).use(hotMiddleware);
app.get('/:viewname?',(req,res,next)=>{
	var viewname = req.params.viewname 
        ?(req.params.viewname)
        : 'index.html';
    var filepath = path.join(compiler.outputPath,'html', viewname);
    // 使用webpack提供的outputFileSystem
    compiler.outputFileSystem.readFile(filepath, function(err, result) {
        if (err) {
            // something error
            return next(err);
        }
        res.set('content-type', 'text/html');
        res.send(result);
        res.end();
    });
})
app.listen(3000,function(err){
	if(err){
		console.log(err);
		return;
	}else{
		var after=conf.isSingle?'':('/'+devstart+'.html')
		var uri = 'http://localhost:' +3000+after;
		openBom&&opn(uri)
	}
});